import Vue from 'vue'
import VueRouter from 'vue-router'
import {
  filterMenu,
  getCookies
} from '@/utils'
import store from '@/store'

import Home from '@/views/Home.vue'
import Login from '@/views/Login'

Vue.use(VueRouter)

const routes = [{
    path: '/home/', // 控制台首页
    component: Home,
    meta: {
      title: '首页',
      icon: 'el-icon-s-home'
    },
    children: [{
      path: '', // 统计页面
      name: 'HomeIndex',
      meta: {
        title: '统计功能',
        icon: 'el-icon-pie-chart',
      },
      component: () => import('@/views/page/Count'),
    }, ],
  },
  {
    path: '/login',
    name: 'Login',
    component: Login,
    meta: {
      title: '登录',
      icon: 'el-icon-user',
      hidden: true
    },
  },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes,
})

const roles = [{
    path: '/goods/', // 商品管理页
    component: Home,
    meta: {
      title: '商品管理',
      icon: 'el-icon-s-shop'
    },
    children: [{
        path: '', // 商品列表页
        name: 'GoodList',
        meta: {
          title: '商品管理',
          icon: 'el-icon-s-data'
        },
        component: () => import('@/views/page/Goods/GoodsList.vue'),
      },
      {
        path: 'add', // 商品添加页
        name: 'GoodAdd',
        meta: {
          title: '商品添加',
          icon: 'el-icon-add-location'
        },
        component: () => import('@/views/page/Goods/GoodsAdd.vue'),
      },
      {
        path: 'edit/:id', // 商品编辑页
        name: 'GoodEdit',
        meta: {
          title: '商品编辑',
          icon: 'el-icon-add-location',
          hidden: true
        },
        component: () => import('@/views/page/Goods/GoodsEdit.vue'),
      },
    ],
  },
  {
    path: '/cate/', // 分类控制
    component: Home,
    meta: {
      title: '分类控制',
      icon: 'el-icon-s-operation'
    },
    children: [{
      path: '', // 分类列表页
      name: 'CateList',
      meta: {
        title: '分类管理',
        icon: 'el-icon-s-open'
      },
      component: () => import('@/views/page/Cate/index.vue'),
    }, ],
  },
  {
    path: '/user/', // 分类控制
    name: 'User',
    component: Home,
    meta: {
      title: '分类管理',
      icon: 'el-icon-user-solid'
    },
    children: [{
      path: 'index', // 分类列表页
      name: 'UserList',
      meta: {
        title: '用户管理',
        icon: 'el-icon-s-tools'
      },
      component: () => import('@/views/page/UserList/index.vue'),
    }, ],
  },
]

let isLoadingMenu = false;

router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    return next()
  } else {
    store.commit('changeUser', getCookies())
    if (
      store.state.user.appkey &&
      store.state.user.username &&
      store.state.user.role
    ) {
      if (!isLoadingMenu) {
        let role = store.state.user.role
        let menu = routes.concat(filterMenu(role, roles))
        store.dispatch('asyncChangeMenu', menu).then(() => {
          menu.forEach((it) => {
            router.addRoute(it)
          })
          isLoadingMenu = true
          if (router.getRoutes().some(it => it.name === to.name)) {
            return next({
              path: to.path,
            })
          } else {
            next({
              name: 'HomeIndex'
            })
          }
        })
      } else {
        if (router.getRoutes().some(it => it.name === to.name)) {
          return next()
        }
      }
    } else {
      return next({
        path: '/login',
      })
    }
  }
  return next()
})

export default router